#include "Field.h"
#include "Vertex.h"

CField::CField(void) : mNumVertices(0), mNumFaces(0), md3dDevice(0), mVB(0), mIB(0)
{
	this->m_vecPosition.x = 0.0f;
	this->m_vecPosition.y = 0.0f;
	this->m_vecPosition.z = 0.0f;

	this->color = DARKGREEN;
}

CField::~CField(void)
{
	ReleaseCOM(mVB);
	ReleaseCOM(mIB);
}

void CField::init(ID3D10Device* device, float scale, D3DXCOLOR COLOR)
{
	ReleaseCOM(mVB);
	ReleaseCOM(mIB);

	this->color = COLOR;

	md3dDevice = device;
 
	mNumVertices = 8;
	mNumFaces    = 12; // 2 per quad

	// Create vertex buffer
    Vertex vertices[] =
    {
		{D3DXVECTOR3(-FieldWidth+this->m_vecPosition.x, -FieldHeight+this->m_vecPosition.y, -FieldWidth+this->m_vecPosition.z), color},
		{D3DXVECTOR3(-FieldWidth+this->m_vecPosition.x, +FieldHeight+this->m_vecPosition.y, -FieldWidth+this->m_vecPosition.z), color},
		{D3DXVECTOR3(+FieldWidth+this->m_vecPosition.x, +FieldHeight+this->m_vecPosition.y, -FieldWidth+this->m_vecPosition.z), color},
		{D3DXVECTOR3(+FieldWidth+this->m_vecPosition.x, -FieldHeight+this->m_vecPosition.y, -FieldWidth+this->m_vecPosition.z), color},
		{D3DXVECTOR3(-FieldWidth+this->m_vecPosition.x, -FieldHeight+this->m_vecPosition.y, +FieldLength+this->m_vecPosition.z), color},
		{D3DXVECTOR3(-FieldWidth+this->m_vecPosition.x, +FieldHeight+this->m_vecPosition.y, +FieldLength+this->m_vecPosition.z), color},
		{D3DXVECTOR3(+FieldWidth+this->m_vecPosition.x, +FieldHeight+this->m_vecPosition.y, +FieldLength+this->m_vecPosition.z), color},
		{D3DXVECTOR3(+FieldWidth+this->m_vecPosition.x, -FieldHeight+this->m_vecPosition.y, +FieldLength+this->m_vecPosition.z), color},
    };

	// Scale the CField.
	for(DWORD i = 0; i < mNumVertices; ++i)
		vertices[i].pos *= scale;


    D3D10_BUFFER_DESC vbd;
    vbd.Usage = D3D10_USAGE_IMMUTABLE;
    vbd.ByteWidth = sizeof(Vertex) * mNumVertices;
    vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = 0;
    vbd.MiscFlags = 0;
    D3D10_SUBRESOURCE_DATA vinitData;
    vinitData.pSysMem = vertices;
    HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));


	// Create the index buffer

	DWORD indices[] = {
		// front face
		0, 1, 2,
		0, 2, 3,

		// back face
		4, 6, 5,
		4, 7, 6,

		// left face
		4, 5, 1,
		4, 1, 0,

		// right face
		3, 2, 6,
		3, 6, 7,

		// top face
		1, 5, 6,
		1, 6, 2,

		// bottom face
		4, 0, 3, 
		4, 3, 7
	};

	D3D10_BUFFER_DESC ibd;
    ibd.Usage = D3D10_USAGE_IMMUTABLE;
    ibd.ByteWidth = sizeof(DWORD) * mNumFaces*3;
    ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
    ibd.CPUAccessFlags = 0;
    ibd.MiscFlags = 0;
    D3D10_SUBRESOURCE_DATA iinitData;
    iinitData.pSysMem = indices;
    HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mIB));
}

void CField::draw()
{
	UINT stride = sizeof(Vertex);
    UINT offset = 0;
    md3dDevice->IASetVertexBuffers(0, 1, &mVB, &stride, &offset);
	md3dDevice->IASetIndexBuffer(mIB, DXGI_FORMAT_R32_UINT, 0);
	md3dDevice->DrawIndexed(mNumFaces*3, 0, 0);
}

D3DXVECTOR3 CField::GetTargetPosition()
{
	return m_vecPosition;
}

void CField::SetPosition(D3DXVECTOR3 &pos)
{
	this->m_vecPosition = pos;
}